package com.vegan_friendly;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;



public class DBAdapter {
	
    private static final String DATABASE_NAME = "vf.db";
    private static final int DATABASE_VERSION = 9;
    private static final String TABLE_NAME = "rest";
    private static final String _ID = "_id";
    public static final String R_RestId 		= "restId";
    public static final String R_Name 		= "name";
    public static final String R_MainPicture 	= "main_picture";
    public static final String R_Phone 	= "phone";
    public static final String R_Address 	= "address";
    public static final String R_OpenHours = "hours";
    public static final String R_Description = "description";
    public static final String R_PicturesUrl = "pictures_url";
    public static final String R_Menu = "menu";
    public static final String R_Site = "site";
    public static final String R_Facebook = "facebook";
    public static final String R_Latitude = "latitude";
    public static final String R_Longitude = "longitude";
    
    public class ColIndex {
    	public static final int ID 				= 0;
    	public static final int RestId 			= 1;
    	public static final int Name 			= 2;
    	public static final int MainPicture 	= 3;
    	public static final int Phone 			= 4;
    	public static final int Address 		= 5;
    	public static final int OpenHours 		= 6;
    	public static final int Description 	= 7;
    	public static final int PicturesUrl		= 8;
    	public static final int Menu 			= 9;
    	public static final int Site 			= 10;
    	public static final int Facebook 		= 11;
    	public static final int Latitude 		= 12;
    	public static final int Longtitue 		= 13;
 
    }
    
    private static final String TAG = "DBAdapter";
    
    private static Context mContext; 
    private static DatabaseHelper mDatabaseHelper;
    
    public static void setContext(Context context){
    	mContext = context;
    }
    
    public static DBAdapter Inst() {
    	if(mInst==null){
    		mInst = new DBAdapter();
    	}
    	return mInst;
    }
    private static DBAdapter mInst = null;
    private DBAdapter() {
    	mDatabaseHelper = new DatabaseHelper(mContext);
    }
    
    public boolean addRests(Document docRests) {
    	
    	NodeList nl =docRests.getElementsByTagName("rest");
    	
    	for(int i=0; i<nl.getLength();i++) {
    		Node nodeRest = nl.item(i);
    		int id = Integer.parseInt(nodeRest.getAttributes().getNamedItem("id").getNodeValue());
    	}
		return false;
    }
    
    public boolean addRest(int restId, String name, String mainPicture, String phone,
    						String address, String openHours, String description,
    						String picturesUrl, String menu, String site, String facebook,
    						double latitude, double longitude){
    	

    	Cursor c = mDatabaseHelper.getReadableDatabase().rawQuery("select "+_ID+" from "+TABLE_NAME+" where "+R_RestId+"=?", new String[]{Integer.toString(restId)});
    	
    	ContentValues cv = new ContentValues();
    	cv.put(R_RestId,restId);
    	cv.put(R_Name,name);
    	cv.put(R_MainPicture,mainPicture);
    	cv.put(R_Phone,phone);
    	cv.put(R_Address,address);
    	cv.put(R_OpenHours,openHours);
    	cv.put(R_Description,description);
    	cv.put(R_PicturesUrl,picturesUrl);
    	cv.put(R_Menu,menu);
    	cv.put(R_Facebook,facebook);
    	cv.put(R_Latitude,latitude);
    	cv.put(R_Longitude,longitude);
    	
    	if(c.getCount() >=1){
    		Log.w("DBAdapter", "another entry found");
    		
    		int id = mDatabaseHelper.getWritableDatabase().update(TABLE_NAME, cv, "where "+R_RestId+"=?", new String[]{Integer.toString(restId)});
    		if(id < 1){
	       		 Log.w(TAG, "error in update " + name);
	       		 return false;
	       	}
    		return true;
    	}

    	long id = mDatabaseHelper.getWritableDatabase().insert(TABLE_NAME, null, cv);
    	if(id==-1){
    		 Log.w(TAG, "error in insert " + name);
    		 return false;
    	}
    	return true;
    }
    
    public Cursor getAllRest(){
    	return mDatabaseHelper.getReadableDatabase().rawQuery("select * from "+ TABLE_NAME, null);
    }
    
    public Cursor getAllRestForList(){
    	return mDatabaseHelper.getReadableDatabase().rawQuery("select " 
    			+ TABLE_NAME + "." + _ID + ", "
    			+ TABLE_NAME + "." + R_MainPicture + ", "
    			+ TABLE_NAME + "." + R_Name + ", "
    			+ TABLE_NAME + "." + R_Address + ", "
    			+ TABLE_NAME + "." + R_Phone 
    			+" from "+ TABLE_NAME, null);
    }
	
    static class DatabaseHelper extends SQLiteOpenHelper {

	public DatabaseHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
           db.execSQL("CREATE TABLE " + TABLE_NAME + " ("
                   + _ID + " INTEGER PRIMARY KEY,"
                   + R_RestId + " INTEGER,"
                   + R_Name + " TEXT,"
                   + R_MainPicture + " TEXT,"
                   + R_Phone + " TEXT,"
                   + R_Address + " TEXT,"
                   + R_OpenHours + " TEXT,"
                   + R_Description + " TEXT,"
                   + R_PicturesUrl + " TEXT,"
                   + R_Menu + " TEXT,"
                   + R_Site + " TEXT,"
                   + R_Facebook + " TEXT,"
                   + R_Latitude + " DOUBLE,"
                   + R_Longitude + " DOUBLE"
                   + ");");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
           Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                   + newVersion + ", which will destroy all old data");

           // Kills the table and existing data
           db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);

           // Recreates the database with a new version
           onCreate(db);
		
	}
}
}

